package com.ruoyi.contact.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contact.service.IContactReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 联系人报表Controller
 * 
 * @author useragent-1
 * @date 2025-06-04
 */
@Controller
@RequestMapping("/contact/report")
public class ContactReportController extends BaseController {

    private String prefix = "contact/report";

    @Autowired
    private IContactReportService contactReportService;

    /**
     * 跳转到报表主页面
     */
    @RequiresPermissions("contact:report:view")
    @GetMapping()
    public String index(ModelMap modelMap) {
        List<Map<String, Integer>> countByCategory = contactReportService.selectContactCountByCategory();
        modelMap.put("dataCountByCategory", countByCategory);
        return prefix + "/report";
    }

    /**
     * 获取月度数据（JSON格式）
     */
    @RequiresPermissions("contact:report:view")
    @GetMapping("/monthlyData")
    @ResponseBody
    public AjaxResult monthlyData() {
        List<Map<String, Object>> data = contactReportService.selectContactCountByMonth();
        List<String> months = new ArrayList<>();
        List<Long> counts = new ArrayList<>();
        for (Map<String, Object> row : data) {
            months.add((String) row.get("month"));
            counts.add(((Number) row.get("count")).longValue());
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("months", months);
        ajax.put("counts", counts);
        return ajax;
    }
}